﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Class.Web.Mvc;
using Telerik.Web.Mvc;
using ThuySan.Areas.Backend.Models;
using ThuySan.Bussiness;
using ThuySan.Models;

#endregion

namespace ThuySan.Areas.Backend.Controllers
{
    [Authorize]
    public class TagController : BaseController
    {
        private readonly UnitOfWork _unitOfWork = new UnitOfWork();

        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        public ActionResult GetList()
        {
            return View(new GridModel(_unitOfWork.TagRepository.Select().Select(c => new {
                                                                                                 c.TagId,
                                                                                                 c.Name,
                                                                                                 IsStaticContent = c.IsStaticContent ?? false
                                                                                                                           ? "checked"
                                                                                                                           : "",
                                                                                                 IsTopMenu = c.IsTopMenu ?? false
                                                                                                                     ? "checked"
                                                                                                                     : "",
                                                                                                 IsLeftColumn = c.IsLeftColumn ?? false
                                                                                                                        ? "checked"
                                                                                                                        : "",
                                                                                                 IsRightColumn = c.IsRightColumn ?? false
                                                                                                                         ? "checked"
                                                                                                                         : "",
                                                                                                 IsFooter = c.IsFooter ?? false
                                                                                                                    ? "checked"
                                                                                                                    : ""
                                                                                         })));
        }

        public ActionResult Add()
        {
            return View(new TagModel());
        }

        [ValidateInput(false), HttpPost]
        public ActionResult Add(TagModel tagModel)
        {
            if (ModelState.IsValid)
            {
                Tag tag = tagModel;

                if (_unitOfWork.TagRepository.CheckInsertTag(tag))
                {
                    Exception exception;
                    tag.PageUrl = ConvertToUnSign(string.IsNullOrEmpty(tagModel.PageUrl)
                                                          ? tag.Name
                                                          : tag.PageUrl);
                    _unitOfWork.TagRepository.Insert(tag, out exception);
                    if (exception == null)
                    {
                        tag.Avatar = SaveResource("thumbnail", "Tag", tag);
                        _unitOfWork.AcceptChanges();
                        return RedirectToAction("Index");
                    }
                }
            }
            return View(tagModel);
        }

        public ActionResult Edit(string id)
        {
            int guid;
            if (int.TryParse(id, out guid))
            {
                Tag tag = _unitOfWork.TagRepository.SelectbyId(guid);
                if (tag != null)
                {
                    TagModel tagModel = tag;
                    return View(tagModel);
                }
            }
            return RedirectToAction("Index");
        }

        [ValidateInput(false), HttpPost]
        public ActionResult Edit(TagModel tagModel)
        {
            if (ModelState.IsValid)
            {
                Tag tag = _unitOfWork.TagRepository.SelectbyId(tagModel.TagId);
                tagModel.Update(tag);
                if (_unitOfWork.TagRepository.CheckUpdateTag(tag))
                {
                    Exception exception;
                    tag.PageUrl = ConvertToUnSign(string.IsNullOrEmpty(tagModel.PageUrl)
                                                          ? tag.Name
                                                          : tag.PageUrl);
                    _unitOfWork.TagRepository.Update(tag, out exception);
                    if (exception == null)
                    {
                        tag.Avatar = SaveResource("thumbnail", "Tag", tag.Avatar, tag);
                        _unitOfWork.AcceptChanges();
                        return RedirectToAction("Index");
                    }
                    ModelState.AddModelError("error", exception.StackTrace);
                }
            }

            return View(tagModel);
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            UpdateResult result = new UpdateResult {
                                                           err = 0,
                                                           module = "Tag",
                                                           msg = "Update Successfully"
                                                   };
            if (id != null)
            {
                string[] s = id.Split(',');
                int guid;
                List<int> list = (from s1 in s where int.TryParse(s1, out guid) select int.Parse(s1)).ToList();

                if (_unitOfWork.TagRepository.CheckDeleteTag(list))
                {
                    _unitOfWork.TagRepository.Delete(list, "Tag", DeleteResource);
                }
            }
            return Json(result, JsonRequestBehavior.DenyGet);
        }
    }
}
